#include "swoole.h"

typedef struct _swPipeUnsock
{
    /**
     * master : socks[1]
     * worker : socks[0]
     */
    int socks[2];
    /**
     * master pipe is closed
     */
    uint8_t pipe_master_closed;
    /**
     * worker pipe is closed
     */
    uint8_t pipe_worker_closed;
} swPipeUnsock;

static int swPipeUnsock_getFd(swPipe *p, int master){
	swPipeUnsock *this = p->object;
	return master == 1 ? this->socks[1] : this->socks[0];
}

int swPipeUnsock_create(swPipe *p, int blocking, int protocol){
	int ret;

	swPipeUnsock *object = malloc(sizeof(swPipeUnsock));
	if(object == NULL){
		return -1;
	}

	bzero(object,sizeof(swPipeUnsock));
	p->blocking = blocking;

	ret = socketpair(AF_UNIX,protocol,0,object->socks);
	if(ret < 0){
		return -1;
	}else{
		p->object = object;
		p->getFd = swPipeUnsock_getFd;
	}
}
